package com.huluobo.a2203kotlindouyindemo.video

import android.graphics.Typeface.CustomFallbackBuilder
import androidx.lifecycle.MutableLiveData
import com.huluobo.base.bean.CommentBeanResult
import com.huluobo.base.bean.CommonBeanResult
import com.huluobo.base.bean.VideoBean
import com.huluobo.base.bean.VideoBeanResult
import com.huluobo.base.model.BaseRepo
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import okhttp3.RequestBody

/**
 *  Created by LC on 2024/7/3.
 */
class VideoRepo : BaseRepo() {
    fun getVideoList(
        currentPage: Int,
        pageSize: Int,
        success: MutableLiveData<VideoBeanResult>,
        failed: MutableLiveData<String>
    ) {
        apiService.getVideoList(currentPage, pageSize)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<VideoBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: VideoBeanResult) {
                    success.value = t
                }
            })
    }

    fun likeVideo(videoId: Int, success: MutableLiveData<CommonBeanResult>, failed: MutableLiveData<String>) {
        apiService.likeVideo(videoId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<CommonBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: CommonBeanResult) {
                    success.value = t
                }
            })
    }

    fun noLikeVideo(videoId: Int, success: MutableLiveData<CommonBeanResult>, failed: MutableLiveData<String>) {
        apiService.noLikeVideo(videoId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<CommonBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: CommonBeanResult) {
                    success.value = t
                }
            })
    }

    fun followAuth(body: RequestBody, success: MutableLiveData<CommonBeanResult>, failed: MutableLiveData<String>) {
        apiService.followAuth(body)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<CommonBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: CommonBeanResult) {
                    success.value = t
                }
            })
    }

    fun getCommentList(videoId: Int, success: MutableLiveData<CommentBeanResult>, failed: MutableLiveData<String>) {
        apiService.getCommentList(videoId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<CommentBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: CommentBeanResult) {
                    success.value = t
                }
            })
    }

    fun sendComment(
        body: RequestBody,
        success: MutableLiveData<CommentBeanResult>,
        failed: MutableLiveData<String>
    ) {
        apiService.sendComment(body)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<CommentBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: CommentBeanResult) {
                    success.value = t
                }

            })
    }

    fun getCollectVideoList(
        videoBean: VideoBean,
        success: MutableLiveData<List<VideoBean>>,
        failed: MutableLiveData<String>
    ) {
        db.getVideoDao().queryVideoList()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<List<VideoBean>> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: List<VideoBean>) {
                    success.value = t
                }
            })
    }
}